import { prisma } from '@/lib/prisma';
import { DestinationCard } from '@/app/components/DestinationCard';
import { DestinationSearch } from '@/app/components/DestinationSearch';

async function getPopularDestinations(searchTerm?: string) {
  return await prisma.destination.findMany({
    where: {
      isOfficial: true,
      ...(searchTerm ? {
        OR: [
          { name: { contains: searchTerm } },
          { description: { contains: searchTerm } }
        ]
      } : {})
    },
    include: {
      _count: {
        select: { activities: true }
      },
      savedBy: true,
    },
    orderBy: {
      createdAt: 'desc'
    }
  });
}

export default async function PopularDestinationsPage({
  searchParams
}: {
  searchParams: { q?: string }
}) {
  const destinations = await getPopularDestinations(searchParams.q);

  return (
    <div className="max-w-7xl mx-auto py-8 px-4 sm:px-6 lg:px-8">
      <div className="mb-8">
        <h1 className="text-3xl font-bold text-blue-900">热门目的地</h1>
        <p className="mt-2 text-gray-600">发现精选的旅游目的地,开启你的下一段旅程</p>
      </div>

      <DestinationSearch />

      {destinations.length > 0 ? (
        <div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
          {destinations.map((destination) => (
            <DestinationCard 
              key={destination.id} 
              destination={destination} 
            />
          ))}
        </div>
      ) : (
        <div className="text-center py-12 bg-gray-50 rounded-lg">
          <p className="text-gray-500">
            {searchParams.q 
              ? '没有找到相关目的地' 
              : '暂无推荐目的地'}
          </p>
        </div>
      )}
    </div>
  );
} 